package de.szut.bomberman.gui.game;

import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;

import de.szut.bomberman.game.logic.Bomb;
import de.szut.bomberman.game.logic.BombExplosion;
import de.szut.bomberman.game.logic.Direction;
import de.szut.bomberman.game.logic.Move;
import de.szut.bomberman.main.GameMode;

public class Listener extends KeyAdapter {

	private Move movementThreadPlayerOne;
	private Move movementThreadPlayerTwo;
	private boolean playerOneThreadIsRunning;
	private boolean playerTwoThreadIsRunning;

	private JLabel playerOne;
	private JLabel playerTwo;

	private ArrayList<JLabel> stonePositions;

	private JLayeredPane contentPane;
	private ArrayList<JLabel> destroyableStones;
	
	private ArrayList<JLabel> bombs;
	
	private GameMode gameMode;

	public Listener(JLabel player1, JLabel player2,
			ArrayList<JLabel> stones,ArrayList<JLabel> destroyableStones, JLayeredPane contentPane, GameMode mode) {
		bombs = new ArrayList<JLabel>();
		playerOne = player1;
		playerTwo = player2;
		gameMode = mode;
		movementThreadPlayerOne = new Move(player1, stones, destroyableStones, bombs);
		movementThreadPlayerTwo = new Move(player2, stones, destroyableStones, bombs);
		movementThreadPlayerOne.start();
		movementThreadPlayerTwo.start();
		playerOneThreadIsRunning = false;
		playerTwoThreadIsRunning = false;
		this.stonePositions = stones;
		this.destroyableStones = destroyableStones;
		this.contentPane = contentPane;
		
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		switch (e.getKeyCode()) {

		case (KeyEvent.VK_RIGHT):
			movementThreadPlayerOne
					.setMovementDirection(Direction.RIGHT);
			if (!playerOneThreadIsRunning) {
				synchronized (movementThreadPlayerOne) {
					movementThreadPlayerOne.proceed();

				}
				playerOneThreadIsRunning = true;
			}
			break;

		case (KeyEvent.VK_LEFT):
			movementThreadPlayerOne.setMovementDirection(Direction.LEFT);
			if (!playerOneThreadIsRunning) {
				synchronized (movementThreadPlayerOne) {
					movementThreadPlayerOne.proceed();

				}
				playerOneThreadIsRunning = true;
			}
			break;

		case (KeyEvent.VK_UP):
			movementThreadPlayerOne.setMovementDirection(Direction.UP);
			if (!playerOneThreadIsRunning) {
				synchronized (movementThreadPlayerOne) {
					movementThreadPlayerOne.proceed();

				}
				playerOneThreadIsRunning = true;
			}
			break;

		case (KeyEvent.VK_DOWN):
			movementThreadPlayerOne.setMovementDirection(Direction.DOWN);
			if (!playerOneThreadIsRunning) {
				synchronized (movementThreadPlayerOne) {
					movementThreadPlayerOne.proceed();

				}
				playerOneThreadIsRunning = true;
			}
			break;

		case (KeyEvent.VK_D):
			movementThreadPlayerTwo
					.setMovementDirection(Direction.RIGHT);
			if (!playerTwoThreadIsRunning) {
				synchronized (movementThreadPlayerTwo) {
					movementThreadPlayerTwo.proceed();

				}
				playerTwoThreadIsRunning = true;
			}
			break;

		case (KeyEvent.VK_A):
			movementThreadPlayerTwo.setMovementDirection(Direction.LEFT);
			if (!playerTwoThreadIsRunning) {
				synchronized (movementThreadPlayerTwo) {
					movementThreadPlayerTwo.proceed();

				}
				playerTwoThreadIsRunning = true;
			}
			break;

		case (KeyEvent.VK_W):
			movementThreadPlayerTwo.setMovementDirection(Direction.UP);
			if (!playerTwoThreadIsRunning) {
				synchronized (movementThreadPlayerTwo) {
					movementThreadPlayerTwo.proceed();

				}
				playerTwoThreadIsRunning = true;
			}
			break;

		case (KeyEvent.VK_S):
			movementThreadPlayerTwo.setMovementDirection(Direction.DOWN);
			if (!playerTwoThreadIsRunning) {
				synchronized (movementThreadPlayerTwo) {
					movementThreadPlayerTwo.proceed();

				}
				playerTwoThreadIsRunning = true;
			}
			break;

		case (KeyEvent.VK_SPACE):
			JLabel bombPlayerTwo = new JLabel(new ImageIcon(
					new Bomb().getTexture()));
			bombPlayerTwo.setSize(50, 50);
			bombPlayerTwo.setLocation(playerTwo.getLocation());
			bombs.add(bombPlayerTwo);
			contentPane.add(bombPlayerTwo, new Integer(2));
			BombExplosion explosionThreadPlayerTwo = new BombExplosion(
					playerOne, playerTwo, bombPlayerTwo, stonePositions, destroyableStones,
					contentPane, bombs);
			explosionThreadPlayerTwo.start();
			break;

		case (KeyEvent.VK_ENTER):
			JLabel bombPlayerOne = new JLabel(new ImageIcon(
					new Bomb().getTexture()));
			bombPlayerOne.setSize(50, 50);
			bombPlayerOne.setLocation(playerOne.getLocation());
			bombs.add(bombPlayerOne);
			contentPane.add(bombPlayerOne, new Integer(2));
			BombExplosion explosionThreadPlayerOne = new BombExplosion(
					playerOne, playerTwo, bombPlayerOne, stonePositions, destroyableStones,
					contentPane, bombs);
			explosionThreadPlayerOne.start();
			break;
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		switch (e.getKeyCode()) {

		case (KeyEvent.VK_RIGHT):
			if (playerOneThreadIsRunning) {
				synchronized (movementThreadPlayerOne) {
					movementThreadPlayerOne.pause();

				}
				playerOneThreadIsRunning = false;
			}
			break;

		case (KeyEvent.VK_LEFT):
			if (playerOneThreadIsRunning) {
				synchronized (movementThreadPlayerOne) {
					movementThreadPlayerOne.pause();

				}
				playerOneThreadIsRunning = false;
			}
			break;

		case (KeyEvent.VK_UP):
			if (playerOneThreadIsRunning) {
				synchronized (movementThreadPlayerOne) {
					movementThreadPlayerOne.pause();

				}
				playerOneThreadIsRunning = false;
			}
			break;

		case (KeyEvent.VK_DOWN):
			if (playerOneThreadIsRunning) {
				synchronized (movementThreadPlayerOne) {
					movementThreadPlayerOne.pause();

				}
				playerOneThreadIsRunning = false;
			}
			break;

		case (KeyEvent.VK_D):
			if (playerTwoThreadIsRunning) {
				synchronized (movementThreadPlayerTwo) {
					movementThreadPlayerTwo.pause();

				}
				playerTwoThreadIsRunning = false;
			}
			break;

		case (KeyEvent.VK_A):
			if (playerTwoThreadIsRunning) {
				synchronized (movementThreadPlayerTwo) {
					movementThreadPlayerTwo.pause();

				}
				playerTwoThreadIsRunning = false;
			}
			break;

		case (KeyEvent.VK_W):
			if (playerTwoThreadIsRunning) {
				synchronized (movementThreadPlayerTwo) {
					movementThreadPlayerTwo.pause();

				}
				playerTwoThreadIsRunning = false;
			}
			break;
		case (KeyEvent.VK_S):
			if (playerTwoThreadIsRunning) {
				synchronized (movementThreadPlayerTwo) {
					movementThreadPlayerTwo.pause();

				}
				playerTwoThreadIsRunning = false;
			}
			break;
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {

	}

}
